<?php

namespace app\common\library\aliyunsms;


class Sms
{
    private $handel;
    private $accessKeyId;
    private $accessKeySecret;
    private $signName;
    private $error;
    private $code = [
        "isp.RAM_PERMISSION_DENY RAM"     => "权限DENY",
        "isv.OUT_OF_SERVICE"              => "业务停机",
        "isv.PRODUCT_UN_SUBSCRIPT"        => "未开通云通信产品的阿里云客户",
        "isv.PRODUCT_UNSUBSCRIBE"         => "产品未开通",
        "isv.ACCOUNT_NOT_EXISTS"          => "账户不存在",
        "isv.ACCOUNT_ABNORMAL"            => "账户异常",
        "isv.SMS_TEMPLATE_ILLEGAL"        => "短信模板不合法",
        "isv.SMS_SIGNATURE_ILLEGAL"       => "信短签名不合法",
        "isv.INVALID_PARAMETERS"          => "参数异常",
        "isp.SYSTEM_ERROR"                => "系统错误",
        "isv.MOBILE_NUMBER_ILLEGAL"       => "非法手机号",
        "isv.MOBILE_COUNT_OVER_LIMIT"     => "手机号码数量超过限制",
        "isv.TEMPLATE_MISSING_PARAMETERS" => "模板缺少变量",
        "isv.BUSINESS_LIMIT_CONTROL"      => "操作太频繁，1条/分钟，5条/小时 ，累计10条/天",//支持1条/分钟，5条/小时 ，累计10条/天
        "isv.INVALID_JSON_PARAM"          => "JSON参数不合法，只接受字符串值",
        "isv.BLACK_KEY_CONTROL_LIMIT"     => "黑名单管控",
        "isv.PARAM_LENGTH_LIMIT"          => "参数超出长度限制",
        "isv.PARAM_NOT_SUPPORT_URL"       => "不支持URL",
        "isv.AMOUNT_NOT_ENOUGH"           => "账户余额不足",

    ];

    public function __construct($config)
    {
        $this->accessKeyId     = $config['accessKeyId'];
        $this->accessKeySecret = $config['accessKeySecret'];
        $this->signName        = $config['signName'];
        $this->handel          = new SignatureHelper();
    }

    /**
     * 发送
     * @param  [type] $data [description]
     * @return [type]       [description]
     */
    public function send($TemplateCode, $mobile, $TemplateParam = [])
    {

        $params                  = [];
        $accessKeyId             = $this->accessKeyId;
        $accessKeySecret         = $this->accessKeySecret;
        $signName                = $this->signName;
        $params["SignName"]      = $signName; //必填: 短信签名
        $params["PhoneNumbers"]  = $mobile; // fixme 必填: 短信接收号码
        $params["TemplateCode"]  = $TemplateCode; //短信模板Code
        $params['TemplateParam'] = $TemplateParam; //设置模板参数
        // $params['OutId']           = ""; //可选: 设置发送短信流水号
        // $params['SmsUpExtendCode'] = ""; //可选: 上行短信扩展码

        // *** 需用户填写部分结束, 以下代码若无必要无需更改 ***
        if(!empty($params["TemplateParam"]) && is_array($params["TemplateParam"])){
            $params["TemplateParam"] = json_encode($params["TemplateParam"], JSON_UNESCAPED_UNICODE);
        }

        // 此处可能会抛出异常，注意catch
        $result = $this->handel->request($accessKeyId, $accessKeySecret, "dysmsapi.aliyuncs.com", array_merge($params, [
                "RegionId" => "cn-hangzhou",
                "Action"   => "SendSms",
                "Version"  => "2017-05-25",
            ]));

        if($result == false){
            return '短信系统异常';
        }
        if($result['Code'] != 'OK'){
            return isset($this->code[$result['Code']]) ? $this->code[$result['Code']] : '错误码：' . $result['Code'];
        }
        return true;
    }

    /**
     * 获取错误信息
     * @return [type] [description]
     */
    public function getError()
    {
        return $this->error;
    }

}